import {addGoodsType, queryGoodsType, modGoodsType, delGoodsType, queryAllGoodsType} from '../services/goods';

export default {
    namespace: 'goodsType',

    state: {
        data: {
            list: [],
            pagination: {},
        },
        typeSList: [],
        loading: true,
    },
    effects: {
        * fetch({payload}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(queryGoodsType, payload);
            yield put({
                type: 'save',
                payload: response,
            });
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },
        // * add({payload, callback}, {call, put}) {
        //     yield put({
        //         type: 'changeLoading',
        //         payload: true,
        //     });
        //     const response = yield call(addGoodsType, payload);
        //     yield put({
        //         type: 'changeLoading',
        //         payload: false,
        //     });
        //
        //     if (callback) callback(response);
        // },
        // * mod({payload, callback}, {call, put}) {
        //     yield put({
        //         type: 'changeLoading',
        //         payload: true,
        //     });
        //     const response = yield call(modGoodsType, payload);
        //     yield put({
        //         type: 'changeLoading',
        //         payload: false,
        //     });
        //
        //     if (callback) callback(response);
        // },
        * del({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(delGoodsType, payload);
            yield put({
                type: 'changeLoading',
                payload: false,
            });

            if (callback) callback(response);
        },
        * queryAll({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(queryAllGoodsType, payload);
            yield put({
                type: 'goodsTypeSave',
                payload: response,
            });
            yield put({
                type: 'changeLoading',
                payload: false,
            });
            if (callback) callback(response);
        },
    },

    reducers: {
        save(state, action) {
            const typeSList = [];
            getTypeSList(typeSList, action.payload.resultData.goodsTypeList.list);
            function getTypeSList(tmpR, list) {
                for (var i = 0; i < list.length; i++) {
                    const typeItem = {value: list[i]["cat_id"], label: list[i]["cat_name"]};
                    if (list[i]["children"]) {
                        typeItem["children"] = [];
                        getTypeSList(typeItem["children"], list[i]["children"]);
                    }
                    tmpR.push(typeItem);
                }
            }
            return {
                ...state,
                typeSList: typeSList,
                data: action.payload.resultData.goodsTypeList,
            };
        },

        goodsTypeSave(state, action) {
            const typeSList = [];
            getTypeSList(typeSList, action.payload.resultData.goodsTypeList);
            function getTypeSList(tmpR, list) {
                for (var i = 0; i < list.length; i++) {
                    const typeItem = {value: list[i]["cat_id"], label: list[i]["cat_name"]};
                    if (list[i]["children"]) {
                        typeItem["children"] = [];
                        getTypeSList(typeItem["children"], list[i]["children"]);
                    }
                    tmpR.push(typeItem);
                }
            }
            return {
                ...state,
                typeSList: typeSList,
                data: action.payload.resultData.goodsTypeList,
            };
        },

        changeLoading(state, action) {
            return {
                ...state,
                loading: action.payload,
            };
        },

    },
};
